package com.shop.dao.imp;
import com.shop.beans.User;
import com.shop.util.JDBCUtil;
import com.shop.beans.Manager;
import com.shop.dao.ManagerDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ManagerDaoImp01 implements ManagerDao {
    @Override
    public void AddManager(Manager manager) {
        try {
            Connection connection=new JDBCUtil().getConnection();
            String sql="insert into t_manager values(null,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1, manager.getManagername());
            ps.setString(2, manager.getManagerpassword());
            ps.setInt(3,manager.getRole());
            ps.execute();
            System.out.println("【INFO】=>ADD-[ACC]添加管理员成功\n\t\t->|"+manager);
            connection.close();
        }
            catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public void UpdateManager(Manager manager) {
        try {
            int id=manager.getId();
            Connection connection= JDBCUtil.getConnection();
            String sql="update t_manager set  managername=? , managerpassword=? , role=?  where id=?";
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setString(1, manager.getManagername());
            ps.setString(2, manager.getManagerpassword());
            ps.setInt(3,manager.getRole());
            ps.setInt(4,id);
            ps.execute();
            System.out.println("【INFO】=>UP-[ACC]修改id为"+id+"管理员名为"+manager.getManagername()+"的管理员成功\n\t\t->|"+manager);
            connection.close();

        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
    @Override
    public void DeleteManager(int id) {
        try{
            Connection connection= JDBCUtil.getConnection();
            String sql="delete  from t_manager where id=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ps.execute();
            System.out.println("【INFO】=>DEL-[ACC]删除id为"+id+"用户名为"+"的用户成功\n\t\t->|");
            connection.close();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
    @Override
    public Manager GetManagerById(int id) {
        Manager  manager1=new Manager();
        try{
            Connection connection=JDBCUtil.getConnection();
            String sql="select * from t_manager where id=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet resultSet=ps.executeQuery();
            while (resultSet.next()){
                 id=resultSet.getInt("id");
                String managername=resultSet.getString("managername");
                String managerpassword =resultSet.getString("managerpassword");
                int role=resultSet.getInt("role");
                Manager manager=new Manager(id,managername,managerpassword,role);
                manager1=manager;
                System.out.println("【INFO】=>GetManager-[ACC]获取管理员成功\n\t\t->|"+manager);
                connection.close();
                break;
            }
            return manager1;

        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
    @Override
    public List<Manager> GetManagers() {
        List<Manager> managerList=new ArrayList<>();
        try{
            Connection connection= JDBCUtil.getConnection();
            String sql="select * from t_manager";
            PreparedStatement ps=connection.prepareStatement(sql);
            ResultSet resultSet= ps.executeQuery();
            System.out.println("【INFO】=>GetManagers-[ACC]获取管理员成功");
            while (resultSet.next()){
                int id=resultSet.getInt("id");
                String managername=resultSet.getString("managername");
                String managerpassword =resultSet.getString("managerpassword");
                int role=resultSet.getInt("role");
                Manager manager=new Manager(id,managername,managerpassword,role);
                managerList.add(manager);
                System.out.println("\t\t->|"+manager);
            }
            System.out.println("\t\t->|共计"+managerList.size());
            connection.close();
            return managerList;
            //ps.execute();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}
